package cn.com.common.job.task;

import java.util.Date;
import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.com.common.constants.Constants;
import cn.com.gas.constants.GasConstants;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationinfoService;
import cn.com.util.RestTemplateUtil;
import cn.com.util.TsBqUtils;

/**
 * 定时任务(演示Demo，可删除) testTask为spring bean的名称， 方法名称必须是run
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Component("gasQianxiStationInfoProcessTask")
@DisallowConcurrentExecution
public class GasQianxiStationInfoProcessTask {
	
	@Value("${gasstation.timedTask.gasQianxiStationInfoProcessTask}")
	private String gasQianxiStationInfoProcessTask;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private GasStationinfoService gasStationinfoService;
	
	@Value("${gasstation.qianxiVideosQueryVideoGetGroupsOfflineUrl}")
	private String url;
	
	
	public void run(String params) throws Exception {
		logger.debug("gasQianxiStationInfoProcessTask定时任务正在执行，参数为：{}", params);
		
		logger.debug("application参数为gasQianxiStationInfoProcessTask：{}", gasQianxiStationInfoProcessTask);

		if (gasQianxiStationInfoProcessTask.equals(Constants.JOB_STOP)) {

			logger.debug("application配置文件控制不执行gasQianxiStationInfoProcessTask定时任务");
			throw new Exception("application配置文件控制不执行gasQianxiStationInfoProcessTask定时任务");
		
		}
		
		
		String domainResponse = RestTemplateUtil.post(url);
		logger.debug("返回报文domainResponse：{}"+domainResponse);
		/*[{
			"id": "130227",
			"name": "迁西县",
			"type": "group",
			"pid": "1302",
			"online": 1
		},
		{
			"id": "13022758",
			"name": "危化企业",
			"type": "group",
			"pid": "130227",
			"online": null
		},
		{
			"id": "130280065",
			"name": "迁西县尹庄百兴加油站",
			"type": "group",
			"pid": "13022758",
			"online": null
		},
		{
			"id": "1212121212",
			"name": "摄像头2",
			"type": "cam",
			"pid": "130280065",
			"online": 1
		},
		{
			"id": "121212111",
			"name": "摄像头1",
			"type": "cam",
			"pid": "130280065",
			"online": 1
		}
	]*/

		if (TsBqUtils.isNotNull(domainResponse)) {
			JSONArray domainJsonArray = JSONArray.parseArray(domainResponse);
			List<GasStationinfoEntity> listDomain = JSONArray.parseArray(JSON.toJSONString(domainJsonArray),
					GasStationinfoEntity.class);
			
			if (TsBqUtils.isNotNull(listDomain)) {
				
				for (GasStationinfoEntity domain : listDomain) {
					saveOrUpdateGasStationinfo(domain);
				}
			}
		}
		
	}
	
	private void saveOrUpdateGasStationinfo(GasStationinfoEntity gasStationinfo){
		Date date = new Date();
		
		String id = gasStationinfo.getId();
		GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(id);
		
		
		if(TsBqUtils.isNotNull(gasStationinfoEntity)){
			
			gasStationinfo.setAddress(gasStationinfoEntity.getAddress());
			gasStationinfo.setTaxorgcode(gasStationinfoEntity.getTaxorgcode());
			gasStationinfo.setIsAnalyse(gasStationinfoEntity.getIsAnalyse());
			
		}
		
		String online = gasStationinfo.getOnline();
		String type = gasStationinfo.getType();
		if("cam".equals(type)){
			if(GasConstants.ON_LINE.equals(online)){
				gasStationinfo.setIconSkin(GasConstants.ICON_SKIN_ON);
			}else{
				gasStationinfo.setIconSkin(GasConstants.ICON_SKIN_OFF);
			}
		}
		
		gasStationinfo.setIsAnalyse("Y");
		
		gasStationinfo.setCreateTime(date);
		gasStationinfo.setDeleted(Constants.DELETEED_NOT);
		gasStationinfoService.saveOrUpdate(gasStationinfo);
	}
	
}
